﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Forums.ForumsClient.Controllers;
using Microsoft.Forums.ForumsClient.Models;

namespace Microsoft.Forums.ForumsClient.Views
{
    /// <summary>
    /// Interaction logic for ForumFilterView.xaml
    /// </summary>
    public partial class ForumFilterView : UserControl
    {

        public static readonly DependencyProperty ContainedViewProperty =
            DependencyProperty.Register(
            "ContainedView",
            typeof(UserControl),
            typeof(ForumFilterView),
            new PropertyMetadata(new PropertyChangedCallback(OnContainerViewChanged)));

        /// <summary>
        /// Gets or sets the contained view.
        /// </summary>
        /// <value>The contained view.</value>
        public UserControl ContainedView
        {
            get
            {
                return (UserControl)this.GetValue(ContainedViewProperty);
            }
            set
            {
                this.SetValue(ContainedViewProperty, value);
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ForumFilterView"/> class.
        /// </summary>
        public ForumFilterView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Called when [container view changed].
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
        static void OnContainerViewChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            ForumFilterView filterView = source as ForumFilterView;
            if (filterView != null)
            {
                filterView.filterableView.Content = e.NewValue;
            }
        }


        /// <summary>
        /// Filter the contained view using the current user filter settings.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void ApplyFilter(object sender, RoutedEventArgs e)
        {
            IFilterableView view = ContainedView as IFilterableView;
            if (view != null)
            {
                view.Filter();
            }
        }

        /// <summary>
        /// Handles the KeyDown event of the searchBox control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Input.KeyEventArgs"/> instance containing the event data.</param>
        private void searchBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                PushSearchTextToDataModel();
                ApplyFilter(null, null);
            }
            else if (e.Key == Key.Escape)
            {
                cancelSearchButton_Click(null, null);
            }
        }

        /// <summary>
        /// Handles the Click event of the searchButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void searchButton_Click(object sender, RoutedEventArgs e)
        {
            PushSearchTextToDataModel();
            ApplyFilter(null, null);
        }

        /// <summary>
        /// Pushes the search text to data model.
        /// </summary>
        private void PushSearchTextToDataModel()
        {
            BindingExpression be = searchBox.GetBindingExpression(TextBox.TextProperty);
            be.UpdateSource();
        }

        /// <summary>
        /// Handles the Click event of the cancelButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void cancelSearchButton_Click(object sender, RoutedEventArgs e)
        {
            IFilterableView filterView = ContainedView as IFilterableView; ;
            if (filterView != null && filterView.FilterSettings != null)
            {
                filterView.FilterSettings.SearchText = String.Empty;
                ApplyFilter(null, null);
            }
        }


    }
}
